package com.weifangfirecontrolformal.weifangfirecontrolformal.web.service.internetofthings.firefightingiot;
import cn.hutool.json.JSONObject;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.base.BaseService;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.model.RespEntity;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.requestutil.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lihaodong
 * ip音柱
 */
@Service
@Slf4j
public class IPSoundColumnService extends BaseService {

    public RespEntity publishOrPlayList(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"publishOrPlayList","IPSoundColumn");
    }

    public RespEntity uploadMediaFile(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"uploadMediaFile","IPSoundColumn");
    }

    public RespEntity publishAudio(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"publishAudio","IPSoundColumn");
    }

    public RespEntity endPlay(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"endPlay","IPSoundColumn");
    }

    public RespEntity getTaskRunInfo(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"getTaskRunInfo","IPSoundColumn");
    }

    public RespEntity deleteMediaFile(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"deleteMediaFile","IPSoundColumn");
    }

    public RespEntity setTaskVolume(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"setTaskVolume","IPSoundColumn");
    }

    public RespEntity showDeviceVolume(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        return fireFightingiotJava(jsonParam,"showDeviceVolume","IPSoundColumn");
    }
    /**
     * splitAry方法<br>
     * @param ary 要分割的数组
     * @param subSize 分割的块大小
     * @return
     *
     */
    private static Object[] splitAry(String[] ary, int subSize) {
        int count = ary.length % subSize == 0 ? ary.length / subSize: ary.length / subSize + 1;

        List<List<String>> subAryList = new ArrayList<List<String>>();

        for (int i = 0; i < count; i++) {
            int index = i * subSize;
            List<String> list = new ArrayList<>();
            int j = 0;
            while (j < subSize && index < ary.length) {
                list.add(ary[index++]);
                j++;
            }
            subAryList.add(list);
        }

        Object[] subAry = new Object[subAryList.size()];

        for(int i = 0; i < subAryList.size(); i++){
            List<String> subList = subAryList.get(i);
            String[] subAryItem = new String[subList.size()];
            for(int j = 0; j < subList.size(); j++){
                subAryItem[j] = subList.get(j);
            }
            subAry[i] = subAryItem;
        }

        return subAry;
    }
}
